package com.mediastory.spsserver.category.dao.impl;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import com.mediastory.spsserver.category.dao.CategoryDao;
import com.mediastory.spsserver.category.domain.Category;
import com.mediastory.spsserver.content.domain.Content;

@Repository
public class CategoryDaoImpl extends SqlSessionDaoSupport  implements CategoryDao {

	public CategoryDaoImpl() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public List<Category> SelectList(Map<String, Object> condition)
			throws Exception {
		// TODO Auto-generated method stub
		
		Integer page = null;
		Integer maxnum = null;
		
		if(condition != null){
			page = (Integer)condition.get("page");
			maxnum = (Integer)condition.get("maxnum");
		}
		
		List<Category> result = null;
		if(page != null && maxnum != null){
			RowBounds rb = new RowBounds(page, maxnum);
			result = getSqlSession().selectList("Category.selectList", condition, rb);
		}else{
			result = getSqlSession().selectList("Category.selectList", condition);
		}
		
		return result;
	}
	
	@Override
	public Integer SelectListTotalCnt(Map<String, Object> condition)
			throws Exception {
		// TODO Auto-generated method stub
		Integer result = getSqlSession().selectOne("Category.selectListTotalCnt", condition);
		return result;
	}

	@Override
	public Category select(Integer id) throws Exception {
		// TODO Auto-generated method stub
		Category result = (Category)(getSqlSession().selectOne("Category.select", id)); 
		return result;
	}

	@Override
	public Category insert(Category category) throws Exception {
		// TODO Auto-generated method stub
		int result = getSqlSession().insert("Category.insert", category);
		return category;
	}

	@Override
	public Category update(Category category) throws Exception {
		// TODO Auto-generated method stub
		int result = getSqlSession().update("Category.update", category);
		return category;
	}

	@Override
	public int delete(Map<String, Object> condition) throws Exception {
		// TODO Auto-generated method stub
		int result = getSqlSession().update("Category.delete", condition);
		return result;
	}

	@Override
	public List<Content> SelectListInCont(Map<String, Object> condition) throws Exception {
		// TODO Auto-generated method stub
		Integer page = (Integer)condition.get("page");
		Integer maxnum = (Integer)condition.get("maxnum");
		
		List<Content> result = null;
		if(page != null && maxnum != null){
			RowBounds rb = new RowBounds(page, maxnum);
			result = getSqlSession().selectList("Category.selectList.inCont", condition, rb);
		}else{
			result = getSqlSession().selectList("Category.selectList.inCont", condition);
		}

		return result;
	}
	
	@Override
	public Integer SelectListInContTotalCnt(Map<String, Object> condition)
			throws Exception {
		// TODO Auto-generated method stub
		Integer result = getSqlSession().selectOne("Category.selectListTotalCnt.inCont", condition);
		return result;
	}

	@Override
	public List<Content> SelectListOutCont(Map<String, Object> condition)
			throws Exception {
		// TODO Auto-generated method stub
		List<Content> result = getSqlSession().selectList("Category.selectList.outCont", condition);
		return result;
	}

	@Override
	public Integer insertDispOrder(Map<String, Object> condition)
			throws Exception {
		// TODO Auto-generated method stub
		int result = getSqlSession().insert("Category.insert.dispOrder", condition);
		return result;
	}

	@Override
	public Integer deleteDispOrder(Map<String, Object> condition) throws Exception {
		// TODO Auto-generated method stub
		int result = getSqlSession().delete("Category.delete.dispOrder", condition);
		return result;
	}

}